package com.group.flagtivities;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class LikeView extends LinearLayout implements OnClickListener {
	
	private TextView dislikeCount;
	private TextView likeCount;
	private ImageView dislike;
	private ImageView like;
	private ProgressBar progressBar;
	
	private OnLikeListener onLikeListener;
	private int dislikes;
	private int likes;
	
	public interface OnLikeListener {
		void onDislike();
		void onLike();
	}
	
	public LikeView(Context context) {
		super(context);
		init();
	}

	public LikeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.likes, this);
		
		findViews();
		configure();
	}
	
	private void findViews() {
		dislikeCount = (TextView) findViewById(R.id.dislikeCount);
		dislike = (ImageView) findViewById(R.id.dislike);
		like = (ImageView) findViewById(R.id.like);
		likeCount = (TextView) findViewById(R.id.likeCount);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
	}
	
	private void configure() {
		dislikes = 0;
		likes = 0;
		dislike.setOnClickListener(this);
		like.setOnClickListener(this);
	}
	
	public void setOnLikeListener(OnLikeListener listener) {
		onLikeListener = listener;
	}
	
	public void setDislikes(int Dislikes) {
		dislikes = Dislikes;
		dislikeCount.setText("" + dislikes);
	}
	
	public void setLikes(int Likes) {
		likes = Likes;
		likeCount.setText("" + likes);
	}
	
	public void setProgressBar() {
		float total = dislikes + likes;
		float progress = dislikes / total * 100;
		
		
		Rect bounds = progressBar.getProgressDrawable().getBounds();
		progressBar.setProgressDrawable(total == 0 ? getResources().getDrawable(R.drawable.background_unliked) : getResources().getDrawable(R.drawable.background_likes));
		progressBar.getProgressDrawable().setBounds(bounds);
		
		progressBar.setProgress(0);
		progressBar.setProgress((int) progress);
	}

	@Override
	public void onClick(View view) {
		switch(view.getId()) {
		case R.id.dislike:
			if (onLikeListener != null) onLikeListener.onDislike();
			break;
		case R.id.like:
			if (onLikeListener != null) onLikeListener.onLike();
			break;
		}
		
	}
}
